package cn.sdhuijin.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * @author sunp@sdhuijin.cn:
 * @version 创建时间：2015年4月29日 上午10:39:07 类说明
 */

@Entity
@Table(name = "standard")
public class Standard extends IdEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	private String name;

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "standard", fetch = FetchType.LAZY)
	private List<Target> target;

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "standard", fetch = FetchType.LAZY)
	private List<ExtraScore> extraScores;

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "standard", fetch = FetchType.LAZY)
	private List<StandardFolder> standardFolder;

	@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinTable(name = "standard_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "standard_id"))
	private List<User> deductionUsers = new ArrayList<User>();

	@ManyToOne
	private User user;

	/**
	 * @return the target
	 */
	public List<Target> getTarget() {
		return target;
	}

	/**
	 * @param target
	 *            the target to set
	 */
	public void setTarget(List<Target> target) {
		this.target = target;
	}

	/**
	 * @return the extraScores
	 */
	public List<ExtraScore> getExtraScores() {
		return extraScores;
	}

	/**
	 * @param extraScores
	 *            the extraScores to set
	 */
	public void setExtraScores(List<ExtraScore> extraScores) {
		this.extraScores = extraScores;
	}

	/**
	 * @return the deductionUsers
	 */
	public List<User> getDeductionUsers() {
		return deductionUsers;
	}

	/**
	 * @param deductionUsers
	 *            the deductionUsers to set
	 */
	public void setDeductionUsers(List<User> deductionUsers) {
		this.deductionUsers = deductionUsers;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the objects
	 */
	public List<Target> getObjects() {
		return target;
	}

	/**
	 * @param objects
	 *            the objects to set
	 */
	public void setObjects(List<Target> objects) {
		this.target = objects;
	}

	/**
	 * @return the standardFolder
	 */
	public List<StandardFolder> getStandardFolder() {
		return standardFolder;
	}

	/**
	 * @param standardFolder
	 *            the standardFolder to set
	 */
	public void setStandardFolder(List<StandardFolder> standardFolder) {
		this.standardFolder = standardFolder;
	}

	public void addDeductionUser(User deductionUser) {
		if (!this.deductionUsers.contains(deductionUser)) {
			this.deductionUsers.add(deductionUser);
			deductionUser.getStandards().add(this);
		}
	}

}
